/**
 * Created by Administrator on 2017/7/19.
 */

var MatchConfig = require('../../../../config/match.json');

module.exports = function(app) {
    return new Handler(app);
};

var Handler = function(app) {
    this.app = app;
};

var handler = Handler.prototype;

/**
 * 获取比赛场信息
 * @param msg
 * @param session
 * @param next
 */
handler.getMatchInfo = function(msg, session, next) {
    var uid = session.uid;

    var resp = {};
    var matchUser = gMatchManager.getUser(uid);
    if (!matchUser) {
        next(null, resp); return;
    }

    resp.basic = matchUser;
    if (matchUser.matchSN) {
        var match = gMatchManager.getMatch(matchUser.matchSN);
        if (match) {
            resp.match = {
                step: match.step,
                user: match.users[uid]
            };
        }
    } else {
        resp.enroll = {
            userCount: gMatchManager.getEnrollUserCount(matchUser.matchId),
            endStamp: gMatchManager.getEndStamp (matchUser.matchId)
        }
    }

    next(null, resp);
};

/**
 * 比赛场报名
 * @param msg
 * @param session
 * @param next
 */
handler.enroll = function(msg, session, next) {
    var uid = session.uid;

    if (!msg.matchId || isNaN(msg.matchId)) {
        next(null, {code: Code.ARGS_INVALID});
        return;
    }

    var matchId = parseInt(msg.matchId);
    var config = MatchConfig[matchId];

    if (!config) {
        next(null, {code: Code.MATCH_ID_INVALID});
        return;
    }

    if (gMatchManager.getUser(uid)) {
        next(null, {code: Code.MATCH_ENROLL_REPEAT});
        return;
    }

    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            next(null, {code: Code.DB_ERROR}); return;
        }

        var code = player.checkResourceCode(config.enrollCost);
        if (code) {
            next(null, {code: code}); return;
        }

        code = gMatchManager.enroll(matchId, uid);
        if (code) {
            next(null, {code: code}); return;
        }

        var round = gMatchManager.getEnrollRound (matchId);

        next(null, {matchId: matchId,
                    userCount: gMatchManager.getEnrollUserCount(matchId),
                    endStamp: gMatchManager.getEndStamp(matchId)});

        player.costResource(config.enrollCost, "matchEnroll");
        gMatchManager.tryStartMatch(matchId);
    });
};

handler.fetchReward = function(msg, session, next) {
    var uid = session.uid;

    if (!uid) {
        next(null, {code: Code.DB_ERROR}); return;
    }

    uid = parseInt(uid);

    gPlayerManger.getPlayer(uid, function(player) {
        if (!player) {
            next(null, {code: Code.DB_ERROR}); return;
        }

        var match = player.user.match;
        if (!match || !match.rank) {
            next(null, {code: Code.MATCH_REWARD_EMPTY}); return;
        }

        if (match.reward) {
            next(null, {code: Code.MATCH_REWARD_FETCHED}); return;
        }

        var matchConfig = MatchConfig[match.matchId];
        if (!matchConfig) {
            next(null, {code: Code.MATCH_ID_INVALID}); return;
        }

        var reward = null;
        for (var i = 0, m = matchConfig.reward.length; i < m; ++i) {
            var rankReward = matchConfig.reward[i];
            if (rankReward.rank > match.rank) {
                break;
            }

            reward = rankReward;
        }

        if (!reward) {
            next(null, {code: Code.MATCH_RANK_NO_REWARD}); return;
        }

        player.addResource(reward.resource, "matchReward");

        next(null, {"resource": reward.resource});
    });
};


handler.leaveEnroll = function(msg, session, next) {
    var uid = session.uid;

    var code = gMatchManager.leaveEnroll(uid);
    next(null, {code:code});
};

